<template>
  <el-dialog title="新增菜单" :visible.sync="dialogVisible" width="30%" :append-to-body="true">
    <el-form ref="addForm" :model="form" :rules="rules" label-width="80px">
      <el-form-item label="菜单名称" prop="name">
        <el-input v-model="form.name"></el-input>
      </el-form-item>
      <el-form-item label="菜单编码" prop="code">
        <el-input v-model="form.code"></el-input>
      </el-form-item>
      <el-form-item label="图标">
        <el-input v-model="form.icon"></el-input>
      </el-form-item>
      <el-form-item label="类型" prop="type">
        <el-radio-group v-model="form.type">
          <el-radio-button label="菜单" />
          <el-radio-button label="按钮" />
        </el-radio-group>
      </el-form-item>
      <el-form-item label="路径">
        <el-input v-model="form.url"></el-input>
      </el-form-item>
      <el-form-item label="备注">
        <el-input type="textarea" v-model="form.note"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit('addForm')">保存</el-button>
        <el-button @click="dialogVisible = false">取 消</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>
<script>
import { baseUrl } from "@/config/env";
const urls = {
  checkField: baseUrl + "/zweixin/common/check_unique", // 验证编码唯一性
  save: baseUrl + "/zweixin/sys/auth/add" // 保存
};
export default {
  data() {
    return {
      dialogVisible: false,
      form: {
        name: null,
        code: null,
        url: null,
        icon: null,
        type: "菜单",
        note: null
      },
      rules: {
        name: [{ required: true, message: "请输入菜单名称", trigger: "blur" }],
        code: [
          { required: true, message: "请输入菜单编码", trigger: "change" },
          { min: 3, max: 3, message: "长度必须为3个字符", trigger: "blur" },
          {
            validator: (rule, value, callback) => {
              axios
                .get(urls.checkField, {
                  params: { table: "t_fk_auth", field: "code", value: value }
                })
                .then(res => {
                  if (res.data.result) {
                    callback("菜单编码必须唯一");
                  } else {
                    callback();
                  }
                });
            },
            trigger: "blur"
          }
        ],
        type: [{ required: true, message: "请选择菜单类型", trigger: "blur" }]
      }
    };
  },
  methods: {
    show() {
      this.dialogVisible = true;
      if (this.$refs["addForm"]) {
        this.$refs["addForm"].resetFields();
      }
    },
    onSubmit(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          axios.post(urls.save, this.form).then(res => {
            this.$message(res.data.result);
            this.dialogVisible = false;
            this.$emit("refresh");
          });
        }
        return false;
      });
    }
  }
};
</script>